package com.ai.service.security.impl;

import com.ai.common.CommonResponse;
import com.ai.entity.security.SysPermission;
import com.ai.entity.security.SysRolePermissionRelation;
import com.ai.mapper.security.SysRolePermissionRelationMapper;
import com.ai.model.param.SysRolePermissionParam;
import com.ai.service.security.ISysPermissionService;
import com.ai.service.security.ISysRolePermissionRelationService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 角色-权限关联关系表 服务实现类
 * </p>
 *
 * @author chen
 * @since 2023-04-04 19:46:07
 */
@Service
public class SysRolePermissionRelationServiceImpl extends ServiceImpl<SysRolePermissionRelationMapper, SysRolePermissionRelation> implements ISysRolePermissionRelationService {


    @Resource
    private ISysPermissionService sysPermissionService;

    /**
     * 更新指定用户的权限
     * @param permissionParam
     * @return
     */
    @Override
    public CommonResponse<String> updateRolePermission(SysRolePermissionParam permissionParam) {


        String roleId = permissionParam.getRoleId();
        //获取当前橘色所拥有的权限
        List<SysRolePermissionRelation> rolePermissionRelationList =
                this.list(new QueryWrapper<SysRolePermissionRelation>().eq("role_id", roleId));

        List<Integer> permissionIdList =permissionParam.getPermissionIdList();


        //首先是权限id集合为空的情况，  即  摘除当前角色的所有的权限
        if (CollectionUtils.isEmpty(permissionIdList)){
            //角色权限列表为空
            //默认只有查看用户的权限，
            SysRolePermissionRelation sysRolePermissionRelation = rolePermissionRelationList.get(0);
            sysRolePermissionRelation.setPermissionId(2);
            this.updateById(sysRolePermissionRelation);

            //其余权限全部删除
            rolePermissionRelationList.remove(0);
            this.removeBatchByIds(rolePermissionRelationList);

            return CommonResponse.ok("数据更新完成");
        }
        /**
         * 权限列表不为空   确定当前所追加权限是否合法
         *
         */
        ArrayList<Integer> permissionFromParam = new ArrayList<>(permissionIdList);//用于运算的权限id集合
        //获取所有权限id
        List<SysPermission> permissionList = sysPermissionService.list();
        List<Integer> permissionDBIds = permissionList.stream().map(SysPermission::getId).collect(Collectors.toList());

        //前端传入的权限id对db中的权限id做差集，  如果还有剩余则说明具有非法权限
        permissionIdList.removeAll(permissionDBIds);
        if (!CollectionUtils.isEmpty(permissionIdList)){
            //不为空，说明具有非法权限
            return  CommonResponse.error("禁止添加非法权限！！！");
        }
        //更新角色权限
        //先删除用角色所关联的所有权限，然后再新增
        this.removeBatchByIds(rolePermissionRelationList);
        LinkedList<SysRolePermissionRelation> sysRolePermissionRelations = new LinkedList<>();
        for (Integer integer : permissionFromParam) {
            SysRolePermissionRelation sysRolePermissionRelation = new SysRolePermissionRelation();
            sysRolePermissionRelation.setRoleId(Integer.valueOf(roleId));
            sysRolePermissionRelation.setPermissionId(integer);
            sysRolePermissionRelations.add(sysRolePermissionRelation);
        }
        //新增
        this.saveBatch(sysRolePermissionRelations);

        return CommonResponse.ok("角色权限维护完成");
    }
}
